library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)

# Wave 2
# Create Bar plot with percentage Movies per age group
summary(factor(df$Q32))
summary(factor(df$AGE7))

df$MOVIES <- NA
df$MOVIES[df$Q32 < 77] <- 0
df$MOVIES[df$Q32 == 2] <- 1
summary(factor(df$MOVIES))

df2 <- df[!is.na(df$MOVIES),]
summary(factor(df2$MOVIES))

# Create Survey design
w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )


# Calculate the means by age
g1 <- "18-24"
g2 <- "25-34"
g3 <- "35-44"
g4 <- "45-54"
g5 <- "55-64"
g6 <- "65-74"
g7 <- "75+"
a1 <- mean(w2_design$variables$MOVIES[w2_design$variables$AGE7==1])
a2 <- mean(w2_design$variables$MOVIES[w2_design$variables$AGE7==2])
a3 <- mean(w2_design$variables$MOVIES[w2_design$variables$AGE7==3])
a4 <- mean(w2_design$variables$MOVIES[w2_design$variables$AGE7==4])
a5 <- mean(w2_design$variables$MOVIES[w2_design$variables$AGE7==5])
a6 <- mean(w2_design$variables$MOVIES[w2_design$variables$AGE7==6])
a7 <- mean(w2_design$variables$MOVIES[w2_design$variables$AGE7==7])

ag <- c(g1,g2,g3,g4,g5,g6,g7)
mn <- c(a1,a2,a3,a4,a5,a6,a7)
movdat <- data.frame(cbind(ag,mn))

b<-barplot(mn, names.arg = ag, 
        xlab = 'Age', 
        ylab='% Gets Most Military Knowledge From Pop Culture',
        ylim=c(0,0.2),
        col = c('gray40','gray40','gray40','gray40','gray40','gray40','gray40'))
text(b, mn+0.005, labels=as.character(round(mn, digits=2)))
